package xpn.platform.modules.sys.user;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.config.Projection;
import xpn.platform.common.repository.XpnRepository;

import java.util.List;
import java.util.Set;

@RepositoryRestResource(path = "users", excerptProjection = DefaultProjection.class)
public interface SysUserDAO extends XpnRepository<SysUser, Long> {

	/**
	 * 根据用户名查找用户
	 *
	 * @param userName
	 * @return
	 */
	@RestResource(path = "byUserName", rel = "byUserName")
	SysUser findOneByUserName(@Param(value = "param") String userName);

	SysUser findOneByName(String name);

	/**
	 * 根据orgId查找用户
	 *
	 * @param orgId
	 * @return
	 */
	@RestResource(path = "byOrgId", rel = "byOrgId")
	List<SysUser> findByOrgId(Long orgId);

	/**
	 * 根据staff_number查询用户
	 *
	 * @return
	 */
	@Query(value = "select o from SysUser o where o.staffNumber=:staffNumber")
	SysUser findOneByStaffNumber(@Param(value = "staffNumber") String staffNumber);


	/**
	 * 返回分页数据示例：这样只产生一条SQL语句
	 *
	 * @return
	 */
	@RestResource(path = "page", rel = "page")
	@Query(value = "select o from SysUser o LEFT JOIN FETCH o.roles roles LEFT JOIN FETCH o.org ou4 LEFT JOIN FETCH ou4.parentOrg ou3  LEFT JOIN FETCH ou3.parentOrg ou2  LEFT JOIN FETCH ou2.parentOrg ou1  LEFT JOIN FETCH ou1.parentOrg ou0", countQuery = "select count(o) from SysUser")
	Page<SysUser> page(Pageable pageable);

	/**
	 * 根据老數據id查找用戶：郑波2018-3-2增加
	 *
	 */
	@RestResource(path = "byOldId", rel = "byOldId")
	SysUser findOneByOldId(@Param(value = "param") String oldId);

//	SysUser findOne(Long id);

}

/**
 * 缺省投影
 *
 * @author bobatkm
 */
@Projection(name = "default", types = { SysUser.class })
interface DefaultProjection {
	Long getId();

	Float getSortNo();

	String getName();

	String getMobile();

	String getUserName();

	String getUserCode();

	String getEnable();

	@Value("#{target.fetchOrgFullName()}")
	String getOrgFullName();

	@Value("#{target.getOrg()==null?null:target.getOrg().getId()}")
	Long getOrgId();
}

/**
 * 列表投影，用于返回关联属性
 *
 * @author bobatkm
 */
@Projection(name = "list", types = { SysUser.class })
interface ListProjection {
	Long getId();

	Float getSortNo();

	String getName();

	String getMobile();

	String getUserName();

	String getUserCode();

	String getEnable();

	@Value("#{target.fetchRoleList()}")
	Set<String> getRoleList();

	@Value("#{target.fetchOrgFullName()}")
	String getOrgFullName();

	@Value("#{target.getOrg()==null?null:target.getOrg().getId()}")
	Long getOrgId();
}

/**
 * 工作流投影，用于返回工作流需要的属性
 *
 * @author bobatkm
 */
@Projection(name = "workflow", types = { SysUser.class })
interface workflowProjection {
	Long getId();

	Float getSortNo();

	String getName();

	String getMobile();

	String getUserName();

	String getUserCode();

	String getEnable();

	@Value("#{target.fetchRoleList()}")
	Set<String> getRoleList();

	@Value("#{target.fetchOrgFullName()}")
	String getOrgFullName();
}
